package com.luxsoft.sw3.gasoc.model;

import java.text.MessageFormat;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.AccessType;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CollectionOfElements;
import org.hibernate.validator.Length;
import org.hibernate.validator.NotEmpty;
import org.hibernate.validator.NotNull;

import com.luxsoft.siipap.model.BaseBean;
import com.luxsoft.siipap.model.UserLog;
import com.luxsoft.siipap.model.core.Cliente;

/**
 * 
 * @author Ruben Cancino
 */
@Entity
@Table(name = "SX_SOCIOS2")
public class Socio  extends BaseBean{

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "SOCIO_ID")
	private Long id;

	@ManyToOne(optional = false)
	@JoinColumn(name = "CLIENTE_ID", nullable = false)
	@NotNull
	private Cliente cliente;

	@Column(name = "APELLIDOP", length = 50)
	@Length(min=1,max=50)
	@NotEmpty
	private String apellidoP;

	@Column(name = "APELLIDOM", length = 50)
	@Length(min=1,max=50)
	@NotEmpty
	private String apellidoM;

	@Column(name = "NOMBRES", length = 150)
	@NotEmpty
	@Length(min=1,max=150)
	private String nombres;

	@Column(name = "SOCIO")
	private String socio;

	@Column(name = "VIGENTE")
	private boolean vigente = false;
	
	@Column(name="TELEFONO1",length=30)
	private String telefono1;
	
	@Column(name="TELEFONO2",length=30)
	private String telefono2;
	
	@Column(name="EMAIL1")
    @Length (max=100)
    private String email;
	
	@Column(name = "PREFERENTE")
	private boolean preferente = true;
	

	@CollectionOfElements (fetch=FetchType.LAZY)
	@JoinTable(
			name="SX_SOCIO_SERVICIOS",
			joinColumns=@JoinColumn(name="SOCIO_ID")	
	)
	private Set<ServicioPorSocio> servicios=new HashSet<ServicioPorSocio>();
	
	@Embedded
	@AttributeOverrides({
			@AttributeOverride(name = "createUser", column = @Column(name = "CREADO_USR", nullable = true, insertable = true, updatable = false)),
			@AttributeOverride(name = "updateUser", column = @Column(name = "MODIFICADO_USR", nullable = true, insertable = true, updatable = true)),
			@AttributeOverride(name = "creado", column = @Column(name = "CREADO", nullable = true, insertable = true, updatable = true)),
			@AttributeOverride(name = "modificado", column = @Column(name = "MODIFICADO", nullable = true, insertable = true, updatable = true)) })
	private UserLog log = new UserLog();

	public String getApellidoM() {
		return apellidoM;
	}

	public void setApellidoM(String apellidoM) {
		Object old = this.apellidoM;
		this.apellidoM = apellidoM;
		firePropertyChange("apellidoM", old, apellidoM);
	}

	public String getApellidoP() {
		return apellidoP;
	}

	public void setApellidoP(String apellidoP) {
		Object old = this.apellidoP;
		this.apellidoP = apellidoP;
		firePropertyChange("apellidoP", old, apellidoP);
	}

	public String getNombres() {
		return nombres;
	}

	public void setNombres(String nombres) {
		Object old = this.nombres;
		this.nombres = nombres;
		firePropertyChange("nombres", old, nombres);
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Cliente getCliente() {
		return cliente;
	}

	public void setCliente(Cliente cliente) {
		Object old=this.cliente;
		this.cliente = cliente;
		firePropertyChange("cliente", old, cliente);
	}

	public String getSocio() {
		return socio;
	}

	public void setSocio(String socio) {
		Object old=this.socio;
		this.socio = socio;
		firePropertyChange("socio", old, socio);
	}

	public boolean isVigente() {
		return vigente;
	}

	public void setVigente(boolean vigente) {
		boolean old=this.vigente;
		this.vigente = vigente;
		firePropertyChange("vigente", old, vigente);
	}

	public Set<ServicioPorSocio> getServicios() {
		return servicios;
	}
	
	public boolean addServicio(ServicioPorSocio s){
		s.setSocio(this);
		return getServicios().add(s);
	}

	public UserLog getLog() {
		if(log==null){
			log=new UserLog();
		}
		return log;
	}

	public void setLog(UserLog log) {
		this.log = log;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((apellidoM == null) ? 0 : apellidoM.hashCode());
		result = prime * result
				+ ((apellidoP == null) ? 0 : apellidoP.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((nombres == null) ? 0 : nombres.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Socio other = (Socio) obj;
		if (apellidoM == null) {
			if (other.apellidoM != null)
				return false;
		} else if (!apellidoM.equals(other.apellidoM))
			return false;
		if (apellidoP == null) {
			if (other.apellidoP != null)
				return false;
		} else if (!apellidoP.equals(other.apellidoP))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (nombres == null) {
			if (other.nombres != null)
				return false;
		} else if (!nombres.equals(other.nombres))
			return false;
		return true;
	}
	
	public String getNombre(){
		String pattern = "{0} {1} {2} ";
        return MessageFormat.format(pattern, getApellidoP(), getApellidoM(), getNombres());
	}

	public String toString(){
		return getNombre();
	}

	public String getTelefono1() {
		return telefono1;
	}

	public void setTelefono1(String telefono1) {
		this.telefono1 = telefono1;
	}

	public String getTelefono2() {
		return telefono2;
	}

	public void setTelefono2(String telefono2) {
		this.telefono2 = telefono2;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}
	
	public boolean isPreferente() {
		return preferente;
	}

	public void setPreferente(boolean preferente) {
		boolean old=this.preferente;
		this.preferente = preferente;
		firePropertyChange("preferente", old, preferente);
	}
	

	

}
